package UI.LessonPlan
{
	import Events.*;
	
	import LessonPlans.*;
	import LessonPlans.Tasks.*;
	
	import Utility.*;
	
	import flash.display.Bitmap;
	
	import mx.core.UIComponent;
	
	public class UILessonPlan extends UIComponent
	{	
		private var titleBox:UIInputBox;
		private var teacherNameBox:UIInputBox;
		private var dateBox:UIInputBox;
		private var taskBoxList:UITaskList;
		
		public function UILessonPlan(lessonPlan:ILessonPlan)
		{
			super();
			titleBox = new UIInputBox("Title","",lessonPlan.title);
			teacherNameBox = new UIInputBox("Teacher","",lessonPlan.teacherName);
			dateBox = new UIInputBox("Date","",DateUtility.dateToString(lessonPlan.date));
			taskBoxList = new UITaskList(lessonPlan.taskList);
			
			addEventListeners();
			positionElements();
			addChildren();
		}
		
		private function addEventListeners():void
		{
			titleBox.addEventListener(UIInputBoxEvent.EXPAND,resizeLessonPlan);
			teacherNameBox.addEventListener(UIInputBoxEvent.EXPAND,resizeLessonPlan);
			dateBox.addEventListener(UIInputBoxEvent.EXPAND,resizeLessonPlan);
			taskBoxList.addEventListener(UIInputBoxEvent.EXPAND,resizeLessonPlan);
		}
		
		private function resizeLessonPlan(e:UIInputBoxEvent):void
		{
			dispatchEvent(new UIInputBoxEvent(e.type));
			positionElements();
		}
		
		public function get lessonPlanData():ILessonPlan
		{
			var d:Date = new Date();
			d.time=Date.parse(dateBox.response);
			var lp:AbstractLessonPlan = new AbstractLessonPlan(titleBox.response,teacherNameBox.response,d);
			
			var taskList:Vector.<Task> = taskBoxList.tasks;
			for each(var task:Task in taskList)
			{
				lp.addTask(task);
			}
			
			return lp;
		}
		
		public function addTask(task:Task):void
		{
			taskBoxList.addTask(task);
			positionElements();
		}
		
		public override function set width(w:Number):void
		{
			titleBox.width = w/3;
			teacherNameBox.width = w/3;
			dateBox.width = w/3;
			taskBoxList.width=w;
			positionElements();
		}
		
		public function clear():void
		{
			taskBoxList.clear();
			positionElements();
		}
		
		public override function get width():Number
		{
			return dateBox.width+dateBox.x;
		}
		
		public override function get height():Number
		{
			return taskBoxList.y+taskBoxList.height;
		}
		
		private function positionElements():void
		{
			teacherNameBox.x = titleBox.width;
			dateBox.x = teacherNameBox.x+teacherNameBox.width;
			taskBoxList.y = Math.max(titleBox.height,dateBox.height,teacherNameBox.height);
			taskBoxList.width = dateBox.width+dateBox.x;
		}
		
		private function addChildren():void
		{
			addChild(titleBox);
			addChild(teacherNameBox);
			addChild(dateBox);
			addChild(taskBoxList);
		}
	}
}